﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CPPEI.Coolzon.Server.CacheData
{
    public class ServerData
    {
        public static readonly string UniqueDomainId = "UniqueDomainId";
        public static bool IsUpdatingSolution = false;
        public static object S_DOMAINLISTLOCKOBJ = new object();

        private static object s_lockObj = new object();
        private static ServerData s_instance;

        public static ServerData Instance
        {
            get
            {
                if (s_instance == null)
                {
                    lock (s_lockObj)
                    {
                        if (s_instance == null)
                        {
                            s_instance = new ServerData();
                        }
                    }
                }
                return s_instance;
            }
        }

        /// <summary>
        /// 控制域列表
        /// </summary>
        private ControlDomain[] _controlDomainList;

        /// <summary>
        /// 未进入任何方案的客户端，客户端登录之后默认进入到这里
        /// </summary>
        private Client[] _clientList;
        private object _clientListLockObj = new object();

        /// <summary>
        /// 方案列表
        /// </summary>
        public List<CPPEI.Coolzon.Metadata.SolutionRef> SolutionList { get; set; }

        private ServerData()
        {
            _controlDomainList = new ControlDomain[10];
            _clientList = new Client[1000];
        }

        #region domain list methods

        public ControlDomain[] GetDomainList()
        {
            return _controlDomainList;
        }

        /// <summary>
        /// 添加domain到内容列表中，同时修改domain的Index属性
        /// </summary>
        /// <param name="domain"></param>
        /// <returns></returns>
        public bool CreateDomain(ControlDomain domain)
        {
            for (int index = 0; index < _controlDomainList.Length; index++)
            {
                if (_controlDomainList[index] == null)
                {
                    _controlDomainList[index] = domain;
                    domain.Index = index;
                    return true;
                }
            }
            return false;
        }

        public bool DeleteDomain(ControlDomain domain)
        {
            if (domain.Index >= 0)
            {
                _controlDomainList[domain.Index] = null;
                domain.Index = -1;
                return true;
            }
            return false;
        }

        public ControlDomain GetDomain(string domainId)
        {
            for (int index = 0; index < _controlDomainList.Length; index++)
            {
                ControlDomain domain = _controlDomainList[index];
                if (domain != null && string.Equals(domain.Id, domainId, StringComparison.CurrentCultureIgnoreCase))
                {
                    return domain;
                }
            }
            return null;
        }

        public ControlDomain GetDomain(int index)
        {
            return _controlDomainList[index];
        }

        /// <summary>
        /// 单域模式时获取唯一存在的域
        /// </summary>
        /// <returns></returns>
        public ControlDomain GetExistsDomain()
        {
            for (int index = 0; index < _controlDomainList.Length; index++)
            {
                ControlDomain domain = _controlDomainList[index];
                if (domain != null)
                {
                    return domain;
                }
            }
            return null;
        }

        #endregion

        #region Client list methods

        public Client[] GetClientList()
        {
            return _clientList;
        }

        public bool AddClient(Client client)
        {
            for (int index = 0; index < _clientList.Length; index++)
            {
                Client item = _clientList[index];
                if (item == null)
                {
                    lock (_clientListLockObj)
                    {
                        if (_clientList[index] == null)
                        {
                            _clientList[index] = client;
                            client.Index = index;
                            return true;
                        }
                    }
                }
                else if(CacheData.CacheDataUtil.RemoveClientIfTimeout(CacheData.ServerData.Instance, item))
                {
                    lock (_clientListLockObj)
                    {
                        if (_clientList[index] == null)
                        {
                            _clientList[index] = client;
                            client.Index = index;
                            return true;
                        }
                    }
                }
            }
            return false;
        }

        public bool RemoveClient(Client client)
        {
            if (client.Index >= 0)
            {
                _clientList[client.Index] = null;
                client.Index = -1;
                return true;
            }
            return false;
        }

        public Client GetClient(string userIdentity)
        {
            for (int index = 0; index < _clientList.Length; index++)
            {
                Client client = _clientList[index];
                if (client != null && client.UserIdentity == userIdentity)
                {
                    return client;
                }
            }
            return null;
        }

        /// <summary>
        /// 通过用户账号获取用户对象
        /// </summary>
        /// <param name="userAccount"></param>
        /// <returns></returns>
        public Client GetClientByAccount(string userAccount)
        {
            for (int index = 0; index < _clientList.Length; index++)
            {
                Client client = _clientList[index];
                if (client != null && client.UserAccount == userAccount)
                {
                    return client;
                }
            }
            return null;
        }

        public Client GetClient(int index)
        {
            return _clientList[index];
        }
        #endregion
    }
}
